<template>
    <view>
        <view class="cu-list menu" :class="class_menu">
            <slot name="list" :list="page.list"></slot>
        </view>
        <modalDialog :show.sync="dialog.show" :title="dialog.title+title" :confirm_title="dialog.title"
            :confirm="confirm" :cancel="cancel" :is_scroll_content="true">
            <template #content>
                <slot name="form" :data.sync="data" :dialog="dialog"></slot>
            </template>
        </modalDialog>
        <foot :title="'添加'+title" :tap="insert"></foot>
    </view>
</template>

<script>
    export default {
        props: ['title', 'url_list', 'url_update', 'form', 'list', 'getList', 'upload', 'class_menu', 'cancel'],
        data() {
            return {
                dialog: {
                    title: '',
                    show: false
                },
                data: {
                    formtag: {},
                    form: {},
                    formbak: {},
                },
                page: {
                    list: []
                }
            };
        },
        created() {
            this.dataFormInit();
            this.pageInit();
        },
        watch: {
            list(new_value) {
                this.page.list = this.list;
            },
            'form.item_id'() {
                this.dataFormInit();
            }
        },
        methods: {
            dataFormInit() {
                this.data.form = this.$nddyny.object.copy(this.form);
                this.data.formbak = this.$nddyny.object.copy(this.form);
                this.uploadInfoInit();
            },
            uploadInfoInit() {
                if (this.upload) {
                    this.upload.info_old = this.$nddyny.object.copy(this.data.form);
                    this.upload.info_new = this.data.form;
                }
            },
            pageInit() {
                if (this.url_list) {
                    this.$api.post(this, {
                        url: this.url_list,
                        success: (Result) => {
                            if (this.$api.form.toast(this, Result)) return;
                            this.page.list = Result.result;
                        }
                    });
                } else {
                    this.page.list = this.list;
                }
            },
            insert() {
                this.confirmBtn();
            },
            update(info) {
                this.confirmBtn(info, 0);
            },
            delete(info) {
                this.confirmBtn(info, 1);
            },
            confirmBtn(info, is_delete) {
                this.dialog.show = true;
                if (info) {
                    if (is_delete == 1) {
                        this.dialog.title = '删除';
                    } else {
                        this.dialog.title = '编辑';
                    }
                    this.data.form = this.$nddyny.object.copy(info);
                    this.data.form.is_delete = is_delete;
                } else {
                    this.dialog.title = '添加';
                    this.data.form = this.$nddyny.object.copy(this.data.formbak);
                }
                this.uploadInfoInit();
            },
            confirm() {
                this.$api.post(this, {
                    url: this.url_update,
                    upload: this.upload,
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        this.dialog.show = false;
                        this.data.form = this.$nddyny.object.copy(this.data.formbak);
                        if (this.getList) {
                            this.getList(Result.result);
                        } else {
                            this.pageInit();
                        }
                    }
                });
            },
        },
    }
</script>
